package com.guigu.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.base.entity.CourierScop;
import com.guigu.base.entity.GlobalUser;
import com.guigu.base.entity.dto.GlobalUserDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.base.mapper.GlobalUserMapper;
import com.guigu.base.service.CourierScopService;
import com.guigu.base.service.GlobalUserService;
import com.guigu.tms.exception.TmsException;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 全局账号 服务实现类
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-18
 */
@Service
public class GlobalUserServiceImpl extends ServiceImpl<GlobalUserMapper, GlobalUser> implements GlobalUserService {


    /**
     * 通过手机 获取验证码
     * TODO 项目后期实现
     *
     * @param phone
     */
    @Override
    public void sendPhoneMessage(String phone) {

    }

    /**
     * 通过邮箱发送验证码
     * <p>
     * TODO 项目后期实现
     *
     * @param mailbox
     */
    @Override
    public void sendMailboxMessage(String mailbox) {

    }

    /**
     * 密码加密
     * <p>
     * TODO 项目后期实现
     *
     * @param password
     * @return
     */
    @Override
    public String passwordEncryption(String password) {
        return password;
    }

    /**
     * 全局用户添加
     *
     * @param globalUserDTO
     * @return
     */
    @Override
    public R addGlobalUser(GlobalUserDTO globalUserDTO) {
        if (ObjectUtil.isEmpty(globalUserDTO)) {
            throw new TmsException(ResultCode.ERROR_CODE, "globalUserDTO对象为null");
        }
        GlobalUser user = convertUser(globalUserDTO);

        /**
         * 添加用户
         */
        int i = this.baseMapper.insert(user);
        return i > 0 ? new R(ResultCode.SUCCESS_CODE, "注册成功!", true) : new R(ResultCode.ERROR_CODE, "注册失败!", false);
    }

    /**
     * 根据快递员Id查询其员工信息
     *
     * @param courierId
     * @return
     */
    @Override
    public GlobalUser queryAccordingToTheCourierId(String courierId) {
        GlobalUser user = this.baseMapper.selectById(courierId);
        return user;
    }

    /**
     * 转换为GlobalUser
     *
     * @param globalUserDTO
     * @return
     */
    @NotNull
    private GlobalUser convertUser(GlobalUserDTO globalUserDTO) {
        /**
         * 转换赋值
         */
        GlobalUser user = new GlobalUser();
        BeanUtil.copyProperties(globalUserDTO, user);
        /**
         * 租户编号为当前时间戳
         */
        user.setTenantCode(String.valueOf(System.currentTimeMillis()));
        /**
         * 创建时,给一个当前时间作为创建时间
         */
        user.setCreateTime(new Date());
        /**
         * TODO 后期修改创建人和修改人
         */
        user.setCreateUser(1L);
        return user;
    }
}
